package com.xingkeduo.pojo.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.query.OfficeQuery;
import com.xingkeduo.pojo.table.OfficeTable;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.domain.Page;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;

public class OfficeJson extends BaseJson {

	public static OfficeQuery createQueryForOffice(String urlParam) throws Throwable {
		JsonNode root = validJson(urlParam);
		if (root == null)
			throw new InputParamException(InputParamException.Type.INVALID, "参数");

		return new OfficeQuery().setId(validId(root, YesNoEnum.YES));
	}


	public static OfficeTable createTableForModifyOffice(String urlParam, OfficeTable officeTable) throws Throwable {
		JsonNode root = validJson(urlParam);
		if (root == null)
			throw new InputParamException(InputParamException.Type.INVALID, "参数");
		officeTable.setId(validId(root, YesNoEnum.YES));
		return officeTable;
	}

	//数据输出格式
	public static ObjectNode toJson(OfficeTable officeTable) {
		ObjectNode root = MAPPER.createObjectNode();

		if (officeTable != null)
		{
			root.put("id", officeTable.getId());
			root.put("vendorId", officeTable.getVendorId());
			root.put("vendorName", officeTable.getVendorName());
			root.put("citiesId", officeTable.getCitiesId());
			root.put("citiesName", officeTable.getCitiesName());
			root.put("code", officeTable.getCode());
			root.put("name", officeTable.getName());
			root.put("manager", officeTable.getManager());
			root.put("phone", officeTable.getPhone());
			root.put("status", officeTable.getStatus());
			//处理时间
			String createdTime =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(officeTable.getCreatedTime());
			root.put("createtime", createdTime);
		}

		return root;
	}

	public static ArrayNode toJson(List<OfficeTable> officeTableList) throws Throwable {
		ArrayNode root = MAPPER.createArrayNode();

		if(!CommonUtil.isEmpty(officeTableList)){
			for (Iterator<OfficeTable> iterator = officeTableList.iterator(); iterator.hasNext();) {
				root.add(toJson(iterator.next()));
			}
		}

		return root;
	}

	public static ObjectNode toJsonPage(Page<OfficeTable> page) throws Throwable {
		ObjectNode root = MAPPER.createObjectNode();
		if (page != null)
		{
			root.put("pageNo", page.getPageNo());
			root.put("pageSize", page.getPageSize());
			root.put("pageTotal", page.getPageTotal());
			root.put("recordTotal", page.getRecordTotal());
			root.put("dtoLists", toJson(page.getDtoList()));
		}
		return root;
	}


}
